package cn.yz.controller;

import cn.yz.dto.MenuDto;
import cn.yz.result.Result;
import cn.yz.service.MenuService;
import cn.yz.service.RoleService;
import cn.yz.vo.MenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理控制器
 */
@Tag(name = "菜单管理", description = "菜单管理相关接口")
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    @Operation(summary = "获取用户菜单列表")
    @GetMapping("/user/{userId}")
    public Result<List<MenuVo>> getUserMenus(@Parameter(description = "用户ID") @PathVariable("userId") String userId) {
        return Result.success(menuService.getUserMenus(userId));
    }

    @Operation(summary = "获取菜单树")
    @GetMapping("/tree")
    public Result<List<MenuVo>> getMenuTree() {
        List<MenuVo> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    @Operation(summary = "获取去除按钮的菜单树")
    @GetMapping("/tree-no-button")
    public Result<List<MenuVo>> getMenuTreeNoButton() {
        List<MenuVo> menuTree = menuService.getMenuTreeNoButton();
        return Result.success(menuTree);
    }

    @Operation(summary = "根据ID获取菜单详情")
    @GetMapping("/{id}")
    public Result<MenuVo> getMenuById(@Parameter(description = "菜单ID") @PathVariable String id) {
        MenuVo menu = menuService.getMenuById(id);
        return Result.success(menu);
    }

    @Operation(summary = "创建菜单")
    @PostMapping
    public Result<Void> createMenu(@RequestBody MenuDto menuDto) {
        menuService.createMenu(menuDto);
        return Result.success();
    }

    @Operation(summary = "更新菜单")
    @PutMapping("/{id}")
    public Result<Void> updateMenu(@Parameter(description = "菜单ID") @PathVariable String id,
                                 @RequestBody MenuDto menuDto) {
        menuService.updateMenu(id, menuDto);
        return Result.success();
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    public Result<Void> deleteMenu(@Parameter(description = "菜单ID") @PathVariable String id) {
        menuService.deleteMenu(id);
        return Result.success();
    }

    @Operation(summary = "获取角色菜单列表")
    @GetMapping("/role/{roleId}")
    public Result<List<MenuVo>> getRoleMenus(@Parameter(description = "角色ID") @PathVariable("roleId") String roleId) {
        List<MenuVo> menus = menuService.getRoleMenus(roleId);
        return Result.success(menus);
    }

    @Operation(summary = "分配角色菜单")
    @PostMapping("/role/{roleId}/assign")
    public Result<Void> assignRoleMenus(@Parameter(description = "角色ID") @PathVariable("roleId") String roleId,
                                      @RequestBody List<String> menuIds) {
        menuService.assignRoleMenus(roleId, menuIds);
        return Result.success();
    }
} 